package com.sunyur.tailor.channel.service.inner.purchase.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.inner.util.SunyurClientUtil;
import com.sunyur.tailor.channel.common.inner.util.SunyurResultUtil;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.purchase.model.PurchaseApplyCreateByOutsideModel;
import com.sunyur.tailor.channel.service.inner.purchase.param.PurchaseApplyCreateByOutsideParam;
import com.sunyur.tailor.channel.service.inner.purchase.param.PurchaseApplyExternalProcessParam;
import com.sunyur.tailor.channel.service.inner.purchase.param.PurchaseApplyRollBackStatusParam;
import com.sunyur.tailor.channel.service.inner.purchase.service.SunyurPurchaseApplyOutsideService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: HuBaoding
 * @Description: 请购单外部处理
 * @Date: Created in 10:11 2021/5/14
 */
@Service("sunyurPurchaseApplyOutsideService")
public class SunyurPurchaseApplyOutsideServiceImpl implements SunyurPurchaseApplyOutsideService {

    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurPurchaseApplyOutsideServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurPurchaseApplyOutsideServiceImpl.class);

    /**
     * 请求商越Client
     */
    @Autowired
    private SunyurClientUtil sunyurClient;

    @Value("${sunyur.api.purchaseApplyOutside}")
    private String purchaseApplyOutside;

    @Value("${sunyur.api.createApplyByOutside}")
    private String createApplyByOutside;

    @Value("${sunyur.api.purchaseApplyRollBackStatus}")
    private String purchaseApplyRollBackStatus;

    /**
     * 请购单外部处理
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> externalProcess(PurchaseApplyExternalProcessParam param) {


        SunyurResult<Boolean> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(purchaseApplyOutside, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("SunyurPurchaseApplyOutsideServiceImpl externalProcess fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("SunyurPurchaseApplyOutsideServiceImpl externalProcess param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 请购单外部创建
     * @param param
     * @return
     */
    @Override
    public ChannelResult<PurchaseApplyCreateByOutsideModel> createApplyByOutside(PurchaseApplyCreateByOutsideParam param) {
        SunyurResult<PurchaseApplyCreateByOutsideModel> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(createApplyByOutside, JSON.toJSONString(param), new TypeReference<SunyurResult<PurchaseApplyCreateByOutsideModel>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("SunyurPurchaseApplyOutsideServiceImpl createApplyByOutside fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("SunyurPurchaseApplyOutsideServiceImpl createApplyByOutside param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

    /**
     * 请购单状态回滚
     * @param param
     * @return
     */
    @Override
    public ChannelResult<Boolean> rollBackStatus(PurchaseApplyRollBackStatusParam param) {
        SunyurResult<Boolean> sunyurResult=null;
        try {
            sunyurResult = sunyurClient.execPost(purchaseApplyRollBackStatus, JSON.toJSONString(param), new TypeReference<SunyurResult<Boolean>>() {});
            // 主线返回错误
            if (!SunyurResultUtil.isSuccess(sunyurResult)) {
                return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(), sunyurResult.getErrorMessage());
            }
        } catch (Exception e) {
            LOGGER_ERROR.error("SunyurPurchaseApplyOutsideServiceImpl rollBackStatus fail,param is {} ",JSON.toJSONString(param));
            return ChannelResultUtil.error(MiddleResultEnum.FAILED.getCode(),e.getMessage());
        }
        LOGGER_INFO.info("SunyurPurchaseApplyOutsideServiceImpl rollBackStatus param is {} , result is {}",JSON.toJSONString(param),JSON.toJSONString(sunyurResult));
        return ChannelResultUtil.success(sunyurResult.getContent());
    }

}
